<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_LV5.8混编版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2022 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: 牧羊人 <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace App\Services;

use App\Models\MenuModel;

/**
 * 菜单管理-服务类
 * @author 牧羊人
 * @since 2020/9/1
 * Class MenuService
 * @package App\Services
 */
class MenuService extends BaseService
{
    /**
     * 构造函数
     * @author 牧羊人
     * @since 2020/9/1
     * MenuService constructor.
     */
    public function __construct()
    {
        $this->model = new MenuModel();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2020/9/1
     * @author 牧羊人
     */
    public function getList()
    {
        // 请求参数
        $param = request()->all();
        // 上级ID
        $pid = getter($param, "pid", 0);
        // 查询条件
        $map = [
            ['pid', '=', $pid],
        ];
        // 部门名称
        $name = getter($param, "name");
        if ($name) {
            $map[] = ['name', 'like', "%{$name}%"];
        }
        $list = $this->model->getList($map, [["sort", "asc"]]);
        if (!empty($list)) {
            foreach ($list as &$val) {
                if ($val['type'] <= 3) {
                    $val['haveChild'] = true;
                } else {
                    $val['haveChild'] = false;
                }
            }
        }
        return message("操作成功", true, $list);
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2020/9/1
     * @author 牧羊人
     */
    public function edit()
    {
        // 请求参数
        $data = request()->all();
        $result = $this->model->edit($data);
        if (!$result) {
            return message("操作失败", false);
        }

        // 上级菜单设置隐藏和显示，默认同步更新子级菜单
        $menuList = $this->model->getChilds($result, true);
        foreach ($menuList as $val) {
            // 设置状态值
            $menuModel = new MenuModel();
            $v = [
                'id' => $val['id'],
                'status' => $data['status'],
                'is_public' => $data['is_public'],
            ];
            $menuModel->edit($v);

            // 获取子级
            $children = $val['children'];
            if (is_array($children) && !empty($children)) {
                foreach ($children as $vt) {
                    $item = [
                        'id' => $vt['id'],
                        'status' => $data['status'],
                        'is_public' => $data['is_public'],
                    ];
                    $menuModel = new MenuModel();
                    $menuModel->edit($item);

                    // 更新子级菜单
                    $children2 = $vt['children'];
                    foreach ($children2 as $vo) {
                        $subItem = [
                            'id' => $vo['id'],
                            'status' => $data['status'],
                            'is_public' => $data['is_public'],
                        ];
                        $menuModel = new MenuModel();
                        $menuModel->edit($subItem);
                    }
                }
            }
        }

        // 节点参数
        $func = isset($data['func']) ? $data['func'] : "";
        // URL地址
        $url = trim($data['url']);
        if ($data['type'] == 3 && $func && $url) {
            $item = explode("/", $url);
            if (count($item) == 3) {

                // 删除以存在的节点
                $funcIds = $this->model->getColumn([
                    ['pid', '=', $result],
                    ['type', '=', 4]
                ], 'id');
                $this->model->deleteAll($funcIds, true);

                // 模块名
                $module = $item[1];
                $funcList = explode(",", $func);
                foreach ($funcList as $val) {
                    $data = [];
                    $data['pid'] = $result;
                    $data['type'] = 4;
                    $data['status'] = 1;
                    $data['is_public'] = 2;
                    $data['sort'] = $val;
                    if ($val == 1) {
                        // 列表
                        $data['name'] = "列表";
                        $data['url'] = "/{$module}/list";
                        $data['permission'] = "sys:{$module}:list";
                    } else if ($val == 5) {
                        // 添加
                        $data['name'] = "添加";
                        $data['url'] = "/{$module}/edit";
                        $data['permission'] = "sys:{$module}:add";
                    } else if ($val == 10) {
                        // 修改
                        $data['name'] = "修改";
                        $data['url'] = "/{$module}/edit";
                        $data['permission'] = "sys:{$module}:edit";
                    } else if ($val == 15) {
                        // 删除
                        $data['name'] = "删除";
                        $data['url'] = "/{$module}/drop";
                        $data['permission'] = "sys:{$module}:drop";
                    } else if ($val == 20) {
                        // 详情
                        $data['name'] = "详情";
                        $data['url'] = "/{$module}/detail";
                        $data['permission'] = "sys:{$module}:detail";
                    } else if ($val == 25) {
                        // 状态
                        $data['name'] = "状态";
                        $data['url'] = "/{$module}/setStatus";
                        $data['permission'] = "sys:{$module}:status";
                    } else if ($val == 30) {
                        // 批量删除
                        $data['name'] = "批量删除";
                        $data['url'] = "/{$module}/batchDrop";
                        $data['permission'] = "sys:{$module}:dall";
                    } else if ($val == 35) {
                        // 添加子级
                        $data['name'] = "添加子级";
                        $data['url'] = "/{$module}/addz";
                        $data['permission'] = "sys:{$module}:addz";
                    } else if ($val == 40) {
                        // 全部展开
                        $data['name'] = "全部展开";
                        $data['url'] = "/{$module}/expand";
                        $data['permission'] = "sys:{$module}:expand";
                    } else if ($val == 45) {
                        // 全部折叠
                        $data['name'] = "全部折叠";
                        $data['url'] = "/{$module}/collapse";
                        $data['permission'] = "sys:{$module}:collapse";
                    } else if ($val == 50) {
                        // 导入数据
                        $data['name'] = "导入数据";
                        $data['url'] = "/{$module}/import";
                        $data['permission'] = "sys:{$module}:import";
                    } else if ($val == 55) {
                        // 导出数据
                        $data['name'] = "导出数据";
                        $data['url'] = "/{$module}/export";
                        $data['permission'] = "sys:{$module}:export";
                    } else if ($val == 60) {
                        // 设置权限
                        $data['name'] = "设置权限";
                        $data['url'] = "/{$module}/permission";
                        $data['permission'] = "sys:{$module}:permission";
                    } else if ($val == 65) {
                        // 设置权限
                        $data['name'] = "重置密码";
                        $data['url'] = "/{$module}/resetPwd";
                        $data['permission'] = "sys:{$module}:resetPwd";
                    }
                    $menuModel = new MenuModel();
                    $menuModel->edit($data);
                }
            }
        }
        return message();
    }
}
